package com.ylbh.modules.shortmessage.controller;

import com.ylbh.common.annotation.SysLog;
import com.ylbh.common.pojo.shortmessage.dto.ShortMessageDTO;
import com.ylbh.common.pojo.shortmessage.vo.SmsSelectTemplateVO;
import com.ylbh.common.utils.CommonResult;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.utils.Ref;
import com.ylbh.modules.shortmessage.entity.AppSmsTemplate;
import com.ylbh.modules.shortmessage.service.ShortMessageService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;


/**
 * @author: czx1
 * @date: 2019/10/08 16:10
 */
@RestController
@RequestMapping("message")
public class ShortMessageController {

    @Autowired
    private ShortMessageService shortMessageService;

    @ApiOperation("根据条件查询出符合用户")
    @GetMapping("queryShortMessageUser")
    public CommonResult queryShortMessageUser(ShortMessageDTO shortMessage) {
        PageUtils page = shortMessageService.queryShortMessageUser(shortMessage);
        return Ref.ok(page);
    }

    @SysLog("短信群发")
    @ApiOperation("短信群发")
    @PostMapping("sendUser")
    @RequiresPermissions("message:sendUser")
    public CommonResult sendUser(ShortMessageDTO shortMessageDTO) {
        shortMessageService.sendUser(shortMessageDTO);
        return Ref.ok();
    }

    @ApiOperation("导出要群发的号码")
    @GetMapping("exportShortMessageUser")
    public CommonResult exportShortMessageUser(HttpServletRequest request, HttpServletResponse response) throws ParseException, IOException {
        shortMessageService.exportShortMessageUser(request, response);
        return Ref.ok();
    }

    @SysLog("阿里云短信模板添加")
    @ApiOperation("阿里云短信模板添加")
    @PostMapping("addTemplate")
    public CommonResult addTemplate(AppSmsTemplate appSmsTemplate) {
        shortMessageService.addTemplate(appSmsTemplate);
        return Ref.ok();
    }

    @ApiOperation("阿里云短信模板查询")
    @RequestMapping("queryTemplateList")
    public CommonResult<List<SmsSelectTemplateVO>> queryTemplateList(){
        return Ref.ok(shortMessageService.queryTemplateList());
    }

    @SysLog("阿里云短信群发")
    @ApiOperation("阿里云短信群发")
    @PostMapping("sendBatchSms")
    public CommonResult sendBatchSms(ShortMessageDTO shortMessageDTO) {
        shortMessageService.sendBatchSms(shortMessageDTO);
        return Ref.ok();
    }
}
